import logging
from socket import gethostname


class HostnameAddingFormatter(logging.Formatter):

    def __init__(self, fmt=None, datefmt=None, style='%'):
        super().__init__(fmt, datefmt, style)

    def format(self, record: logging.LogRecord) -> str:
        try:
            record.__dict__['hostname'] = gethostname()
        except:
            record.__dict__['hostname'] = 'exception-getting-hostname'
        s = super().format(record)
        return s
